/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    $.ajax({
        type: apis.adminIndex.method,
        url: apis.adminIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#pageTotal').html(res.total);
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].name + '</td>';
                h += '<td>' + d[i].auth_name + '</td>';
                h += '<td>' + d[i].lasttime + '</td>';
                h += '<td class="f-14">';
                h += '<a title="编辑" href="javascript:;" onclick="admin_edit(\'编辑\',\'admin-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
                h += '修改 <i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                if (d[i].id != 1) {
                    h += ' &nbsp; ';
                    h += '<a href="javascript:;" onclick="admin_auth(\'权限分配\',\'admin-auth.html\', ' + d[i].id + ')">';
                    h += '权限分配';
                    h += '</a>';
                }
                h += ' &nbsp; ';
                h += '<a title="密码" href="javascript:;" onclick="admin_pwd(\'密码\',\'admin-pwd.html\', ' + d[i].id + ')" style="text-decoration:none">';
                h += '密码 <i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].name + '"/>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(v){
    $.ajax({
        type: apis.adminOne.method,
        url: apis.adminOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            $('#title')[0].value = res.data.name;
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function admin_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,300);
}


//密码
function admin_pwd(title,url,id,w,h){
    var tt = $('#title' + id).val();
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//保存更新
function save(){
    var formData = {
        id: id,
        title: $('#title').val(),
    };
    if (!formData.title) {
        msgPop({msg: '功能名称必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.adminSave.method,
        url: apis.adminSave.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//更新密码
function setPwd(){
    var formData = {
        id: id,
        pwd: $('#pwd').val(),
        pwd2: $('#pwd2').val(),
    };
    if (!formData.id) {
        msgPop({msg: 'ID必须的'}); return
    }
    if (!formData.pwd) {
        msgPop({msg: '密码必须的'}); return
    }
    if (!formData.pwd2) {
        msgPop({msg: '新密码必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.adminPwd.method,
        url: apis.adminPwd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//权限分配
function admin_auth(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取权限列表
function authList(){
    var formData = {
        aid: aid,
    };
    $.ajax({
        type: apis.authList.method,
        url: apis.authList.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i, ii, child;
            for (i = 0; i < d.length; i ++) {
                //if (d[i].ia_auth == 1) {
                //    h += '<div class="lisel">';
                //    h += '<input type="checkbox" checked/> ';
                //} else {
                //    h += '<div class="li">';
                //    h += '<input type="checkbox"/> ';
                //}
                //h += d[i].action_name;
                //h += '<input type="hidden" id="aid' + a[i].admin_id + '" value=""/>';
                //h += '</div>';
                //child = d[i].childs;
                //if (child.length > 0) {
                //    var gaodu = Math.ceil(child.length / 2) * 35;
                //    h += '<div style="margin-left: 50px; margin-bottom: 50px; width: 100%; height: ' + gaodu + 'px;">';
                //    for (ii = 0; ii < child.length; ii ++) {
                //        if (child[ii].is_auth == 1) {
                //            h += '<div class="litdsel">';
                //            h += '<input type="checkbox" checked/> ';
                //        } else {
                //            h += '<div class="litd">';
                //            h += '<input type="checkbox"/> ';
                //        }
                //        h += child[ii].action_name;
                //        h += '</div>';
                //    }
                //}
                //h += '</div>';
                ////////////////////////////////////////
                h += '<tr class="text-c">';
                h += '<td style="text-align: left;">';
                if (d[i].is_auth == 1) {
                    h += '<div class="td tdsel" style="width: 80%;" onclick="setAuth(' + d[i].id + ');"> ';
                    h += '<input type="checkbox" checked/> ';
                } else {
                    h += '<div class="td" style="width: 80%;" onclick="setAuth(' + d[i].id + ');"> ';
                    h += '<input type="checkbox"/> ';
                }
                h += d[i].title;
                h += '</div>';
                h += '</td>';
                h += '</tr>';
                child = d[i].childs;
                if (child.length > 0) {
                    h += '<tr class="text-c">';
                    h += '<td style="padding-left: 50px; text-align: left;">';
                    for (ii = 0; ii < child.length; ii ++) {
                         if (child[ii].is_auth == 1) {
                             h += '<div class="td tdsel" onclick="setAuth(' + child[ii].id + ');"> ';
                             h += '<input type="checkbox" checked/> ';
                         } else {
                             h += '<div class="td" onclick="setAuth(' + child[ii].id + ');"> ';
                             h += '<input type="checkbox"/> ';
                         }
                        h += child[ii].title;
                        h += '</div>';
                    }
                    h += '</td>';
                    h += '</tr>';
                }
                h += '';
                h += '';
                h += '';
            }
            $('tbody').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//设置权限
function setAuth(v){
    var formData = {
        id: v, aid: aid,
    };
    $.ajax({
        type: apis.authModify.method,
        url: apis.authModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '配置成功'});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}